#pragma once

#include <windows.h>

class rect
{
public:
	rect()
	{
		m_left = 0;
		m_right = 0;
		m_top = 0;
		m_bottom = 0;
	}
	rect(u32 _left,u32 _top,u32 _right,u32 _bottom)
	{
		m_left = _left;
		m_right = _right;
		m_top = _top;
		m_bottom = _bottom;
	}
	u32 m_left;
	u32 m_right;
	u32 m_top;
	u32 m_bottom;
};

class color
{
public:
	color()
	{
		m_r = 255;
		m_g = 255;
		m_b = 255;
		m_a = 255;
	}
	color(u8 _r,u8 _g,u8 _b,u8 _a=255)
	{
		m_r=_r;
		m_g=_g;
		m_b=_b;
		m_a=_a;
	}
	static const color red(){return color(255,0,0);}
	static const color green(){return color(0,255,0);}
	static const color blue(){return color(0,0,255);}
	static const color gray(){return color(50,50,50);}

	u8 m_r;
	u8 m_g;
	u8 m_b;
	u8 m_a;
};

class CTimer
{
public:
	CTimer()
	{
		QueryPerformanceFrequency((LARGE_INTEGER*)&m_freq);
		QueryPerformanceCounter((LARGE_INTEGER*)&m_time);
	}
	~CTimer()
	{
	}
	u64 Time()
	{
		u64 count;
		QueryPerformanceCounter((LARGE_INTEGER*)&count);
		return (1000*((count-m_time)/m_freq));
	}

private:
	u64 m_time;
	u64 m_freq;
};

class stream
{
public:
	stream(const char* _fname,bool _write=false)
	{
		m_size = 0;
		m_pos  = 0;
		m_file = INVALID_HANDLE_VALUE;

		m_file = CreateFile(_fname,_write?GENERIC_ALL:GENERIC_READ,
							  _write?FILE_SHARE_WRITE|FILE_SHARE_READ:FILE_SHARE_READ,NULL,
							  _write?CREATE_ALWAYS:OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

		if(m_file != INVALID_HANDLE_VALUE)
		{
			m_size = GetFileSize(m_file,NULL);
		}
	}
	~stream()
	{
		if(m_file != INVALID_HANDLE_VALUE)
			CloseHandle(m_file);
	}
	void SetPos(u32 _pos)
	{
		SetFilePointer(m_file,_pos,NULL,FILE_BEGIN);
		m_pos = _pos;
	}
	u32 GetPos()
	{
		return m_pos;
	}
	u32 Read(void* _buf,u32 _size)
	{
		ASSERT(m_size!=0,"Bad file");
		DWORD tmp_rb = 0;
		ReadFile(m_file,_buf,_size,&tmp_rb,NULL);
		m_pos += tmp_rb;
		return tmp_rb;
	}

private:
	u32 m_size;
	u32 m_pos;
	HANDLE m_file;
};